﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdateContactHandler : ICommandHandler<CreateOrUpdateContactCommand>
    {
        private readonly IContactRepository contactRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateContactHandler(IContactRepository contactRepository, IUnitOfWork unitOfWork)
        {
            this.contactRepository = contactRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateContactCommand command)
        {
            var contact = new Contact
            {
                ID = command.ID,
                Code=command.Code,
                ContactName=command.ContactName,
                Description = command.Description,
                Email=command.Email,
                Fax_Num=command.Fax_Num,
                PostAddress=command.PostAddress,
                PostAttention=command.PostAttention,
                PostCity=command.PostCity,
                PostCountry=command.PostCountry,
                Website=command.Website,
                Type=command.Type,
                Tel_Num=command.Tel_Num
            };
            if (contact.ID == 0)
                contactRepository.Add(contact);
            else
                contactRepository.Update(contact);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateContactCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateContactCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
